#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QString>
#include<QDebug>
#include<QPaintEvent>
#include<QSound>
#include<smoke.h>
#include<QCoreApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    paintEvent(this->e);
    in();//初始化
    game();//初始化游戏
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::in()
{
    update();
    //QSound::play();//播放音乐
    //    this->move(400,300);
    this->setFixedSize(1500,900);
    qDebug()<<"初始化";
    QPainter painter(this);

    QImage ground(":/Saved Pictures/litanggrass.jpg");
    //if()

    ground=ground.scaled(1500,900);//缩放
    int groundHeight=ground.height();
    QImage back("://Saved Pictures/furongdao.jpg");
    back=back.scaled(1500,120);//缩放
    painter.drawImage(0,0,ground);
    painter.drawRect(0,780,1500,120);


    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
}

void MainWindow::speed()
{
    downSpeed=10.0;
    upSpeed=30.0;
}

void MainWindow::game()
{
    in();
    speed();
    dzStatus=DZ::down;
    timer=new QTimer(this);
    timer->start(16);

}

void MainWindow::endgame()
{
    timer->stop();
    gameStatus=GAME::STOP;
    dzy=0.0;

}

void MainWindow::paintEvent(QPaintEvent *ev)
{
    in();
    qDebug()<<"绘图";
    backdraw();
    smokedraw();
    dzdraw();//绘制背景图，芙蓉王障碍，丁真角色
}

void MainWindow::backdraw()//绘制背景
{
    QPainter painter(this);
    int height=ground.height();
    int pos;
    QImage back("://Saved Pictures/furongdao.jpg");

    back=back.scaled(1500,120);//缩放
    painter.drawImage(0,780,back);
}

void MainWindow::smokedraw()
{
    QPainter painter(this);
    //furong=QPixmap(QApplication::applicationDirPath().append(""));
}

void MainWindow::dzdraw()//绘制角色
{


    dz1=new QPixmap("://Saved Pictures/dingzhen.jpg");
    dz3=new QPixmap("://Saved Pictures/dingzhencry.jpg");


    QPainter painter(this);

    QPixmap dz("://Saved Pictures/dingzhen.jpg");//绘制丁真
    dz=dz.scaled(60,80);//缩放

    if(dzStatus==DZ::down)
    {
        dzy+=downSpeed;//实现下降绘制
        downSpeed+=1;//下降加速度

    }
    if(dzStatus==DZ::up)
    {
        dzy=dzy-upSpeed;
        upSpeed-=3.0;

        if(upSpeed<=0.0)
        {
            dzStatus=DZ::down;
            speed();
        }
    }

    dzx=500;
    painter.drawPixmap(dzx,dzy,dz);
    if(dzy>=780)
    {
        qDebug()<<dzy;
        dz=*dz3;
        painter.drawPixmap(100,100,dz);
        endgame();
    }

}

void MainWindow::mousePressEvent(QMouseEvent *)
{
    qDebug()<<"点击";
    //QSound::play(WINGPATH);//添加声音，每次点击
    dzStatus=DZ::up;
    in();
    speed();
}
void MainWindow::cirPaint()
{
    update();
}

void MainWindow::cirgame()
{
    game();
}


